/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package opensocial.apps.friendforsale.command;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import opensocial.apps.common.Message;
import opensocial.apps.friendforsale.model.Activity;
import opensocial.apps.friendforsale.model.Player;

import org.apache.log4j.Logger;

/**
 * return player infomation include slaves,activities
 * 
 * @author Sirius
 * 
 */
public class GetPlayerCommand extends FriendsaleBaseCommand {
  private static final Logger LOGGER = Logger.getLogger(GetPlayerCommand.class);

  @Override
  public boolean execute() throws Exception {
    String playerId = request.getParameter("id");

    if (playerId == null) {
      playerId = vid;
    }

    Player p = this.getPlayer(playerId);

    if (p == null) {
      LOGGER.error("not found player:" + ctn + " " + playerId);
      output(new Message(MessageId.PLAYER_NOT_FOUND, null));
      return END;
    }

    this.hasError(false);

    Map<String, Object> out = new HashMap<String, Object>();
    out.put(p.getUserId(), p);
    List<Player> slaves = persist.loadSlaves(p);
    if (slaves != null && slaves.size() > 0) {
      out.put("slaves", slaves);
    }

    List<Activity> activitys = persist.loadActivity(p);
    if (activitys != null && activitys.size() > 0) {
      out.put("activities", activitys);
    }

    output(out);
    LOGGER.info("loaded player:" + ctn + " " + playerId);
    return NEXT;
  }
}
